package cn.afterturn.easypoi.word;

import cn.afterturn.easypoi.word.entity.bookmark.*;
import cn.afterturn.easypoi.word.parse.BookmarkHandle;
import cn.afterturn.easypoi.word.parse.BookmarkHandleFromWordEntityToMap;
import cn.afterturn.easypoi.word.vo.WordPage;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;

import static org.junit.Assert.assertTrue;

public class WordService {
    public WordPage getwordParseBookMark(String url) throws Exception{
        WordEntityVO wordEntityVO = cn.afterturn.easypoi.word.WordExportUtilExtH.getWord07BookMark(
                url);
        WordPage wordPage= WordEntityPrint.exportToWordPage(wordEntityVO);
        System.out.println(wordPage.getNormalParagraphList().size()+"---");
        System.out.println(wordPage.getTableParaList().size());
        return wordPage;
    }
    /*
     * 对WordEntityVO处理采用排序及从单项关联设置为双向关联
     * */
    public  void getwordParseBookMarkWordEntity(WordEntityVO wordEntityVO) throws Exception{
        sortwordentityvo(wordEntityVO);
        setBirelation(wordEntityVO);

    }
    public WordEntityVO getwordParseBookMarkWordEntity(String url) throws Exception{

        WordEntityVO wordEntityVO = cn.afterturn.easypoi.word.WordExportUtilExtH.getWord07BookMark(
                url);

        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(wordEntityVO);
        int beforeSort=jsonString.length();
        System.out.println("当前的原始输出，排序前\r\n");
        System.out.println(jsonString);
        sortwordentityvo(wordEntityVO);
        jsonString = mapper.writeValueAsString(wordEntityVO);
        int afterSort=jsonString.length();
        System.out.println("排序之后的输出：\r\n");
        System.out.println(jsonString);
        assertTrue(beforeSort==afterSort);
        setBirelation(wordEntityVO);
        jsonString = mapper.writeValueAsString(wordEntityVO);
        mapper.writeValue(new File("wordEntityVO.json"), wordEntityVO);
        //保存文件
        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";
        String responsefile=relativefilePath+"response.txt";
        File fp=new File(responsefile);
        PrintWriter pfp= new PrintWriter(fp);
        pfp.print(jsonString);
        pfp.close();

        return wordEntityVO;
    }
    private void removeParaNull(WordEntityVO wordEntityVO){
        wordEntityVO.getTableEntityVOS().forEach(tableEntityVO -> {
            tableEntityVO.getRowEntityVOS().forEach(rowEntityVO -> {

            });
        });
    }
    private void setParagraphBirelation(ParagraphEntityVO paragraphEntityVO){
        paragraphEntityVO.getBookMarkEntityVOS().forEach(bookMarkEntityVO -> {
            bookMarkEntityVO.setParagraphEntityVO(paragraphEntityVO);
            bookMarkEntityVO.getMultiValueEntityVOS().forEach(multiValueEntityVO -> {
                multiValueEntityVO.setBookMarkEntityVO(bookMarkEntityVO);
            });
        });
    }
    private void setBirelation(WordEntityVO wordEntityVO){
        //第一层设置
        wordEntityVO.getHeaderEntityVOS().forEach(x->{
            x.setWordEntityVO(wordEntityVO);
            x.getParagraphEntityVOS().forEach(paragraphEntityVO -> {
                paragraphEntityVO.setHeaderEntityVO(x);
                setParagraphBirelation(paragraphEntityVO);
            });
        });
        wordEntityVO.getParagraphEntityVOS().forEach(x->{
            x.setWordEntityVO(wordEntityVO);
            setParagraphBirelation(x);
        });
        wordEntityVO.getTableEntityVOS().forEach(tableEntityVO->{
            tableEntityVO.setWordEntityVO(wordEntityVO);
            tableEntityVO.getRowEntityVOS().forEach(rowEntityVO -> {
                rowEntityVO.setTableEntityVO(tableEntityVO);
                rowEntityVO.getCellEntityVOS().forEach(cellEntityVO -> {
                    cellEntityVO.setRowEntityVO(rowEntityVO);
                    cellEntityVO.getParagraphEntityVOS().forEach(paragraphEntityVO -> {
                        paragraphEntityVO.setCellEntityVO(cellEntityVO);
                        setParagraphBirelation(paragraphEntityVO);
                    });
                });
            });
        });
        wordEntityVO.getFootEntityVOS().forEach(x->{
            x.setWordEntityVO(wordEntityVO);
            x.getParagraphEntityVOS().forEach(paragraphEntityVO -> {
                paragraphEntityVO.setFootEntityVO(x);
                setParagraphBirelation(paragraphEntityVO);
            });
        });



    }
    //提供接口给客户端
    //用户端传入文件，后缀是docx，运行解析为wordPage，并进行返回。
    public WordPage getwordParseBookMark(InputStream inputStream,String url) throws Exception{

        WordEntityVO wordEntityVO = cn.afterturn.easypoi.word.WordExportUtilExtH.getWord07BookMark(
                inputStream,url);
        WordPage wordPage= WordEntityPrint.exportToWordPage(wordEntityVO);
        System.out.println(wordPage.getNormalParagraphList().size()+"---");
        System.out.println(wordPage.getTableParaList().size());
        return wordPage;
    }
    /*用户端传入文件，后缀是docx，传入wordPage，服务端进行检查后发生客户端
    * */
    public Map<String,Object> setMapFromWordPage(WordPage wordPage) {
        BookmarkHandle bookmarkHandle = new BookmarkHandle();
        bookmarkHandle.isFromNetWork = true;
        Map<String, Object> mapNormal = bookmarkHandle.handleNormalParagraph(wordPage.getNormalParagraphList());
        Map<String, Object> mapTable = bookmarkHandle.handleTableParagraphExt(wordPage.getTableParaList());
        Map<String, Object> mapcombined = bookmarkHandle.mapcombined(mapNormal, mapTable);
        return mapcombined;
    }
    /*用户端传入文件，后缀是docx，传入wordPage，服务端进行检查后发生客户端
     * */
    public Map<String,Object> setMapFromWordPage(WordEntityVO wordEntityVO) {
        BookmarkHandleFromWordEntityToMap bookmarkHandle = new BookmarkHandleFromWordEntityToMap();


        Map<String, Object> mapcombined = bookmarkHandle.getMap(wordEntityVO);
        return mapcombined;
    }

    private void sortwordentityvo(WordEntityVO wordEntityVO) throws Exception{
        //对ParagraphEntity进行排序，从list渠道到Treeset
        List<ParagraphEntityVO> paragraphEntityVOS=wordEntityVO.getParagraphEntityVOS().stream().sorted(Comparator.comparing(ParagraphEntityVO::getParaSequence)).collect(Collectors.toList());
        Set<ParagraphEntityVO> paragraphEntityVOS1  = new TreeSet<>(new Comparator<ParagraphEntityVO>() {
            @Override
            public int compare(ParagraphEntityVO o1, ParagraphEntityVO o2) {
                return o1.getParaSequence()-o2.getParaSequence();
                 }
        });
            paragraphEntityVOS.forEach(x -> {
                paragraphEntityVOS1.add(x);
            });
            //把原来的段落替换掉
        wordEntityVO.setParagraphEntityVOS(paragraphEntityVOS1);
        //对TableEntity进行排序，从HashSet到TreeSet
        Set<TableEntityVO> tableEntityVOS=wordEntityVO.getTableEntityVOS();
        Set<TableEntityVO> tableEntityVOS1  = new TreeSet<>(new Comparator<TableEntityVO>() {
            @Override
            public int compare(TableEntityVO o1, TableEntityVO o2) {
                return o1.getTableSequence()-o2.getTableSequence();
            }
        });

        tableEntityVOS.forEach(tableEntityVO -> {
                Set<RowEntityVO> rowEntityVOS1  = new TreeSet<>(new Comparator<RowEntityVO>() {
                    @Override
                    public int compare(RowEntityVO o1, RowEntityVO o2) {
                        return o1.getRowIndex()-o2.getRowIndex();
                    }
                });
                tableEntityVO.getRowEntityVOS().forEach(rowEntityVO -> {
                            Set<CellEntityVO> cellEntityVOS1  = new TreeSet<>(new Comparator<CellEntityVO>() {
                                @Override
                                public int compare(CellEntityVO o1, CellEntityVO o2) {
                                    return o1.getCellVertical()-o2.getCellVertical();
                                }
                            });
                            rowEntityVO.getCellEntityVOS().forEach(cellEntityVO -> {

                                cellEntityVOS1.add(cellEntityVO);
                            });
                            rowEntityVO.setCellEntityVOS(cellEntityVOS1);
                            rowEntityVOS1.add(rowEntityVO);
                            //cellEntityVOS1.clear();
                        });
                    tableEntityVO.setRowEntityVOS(rowEntityVOS1);
                    //rowEntityVOS1.clear();
                    tableEntityVOS1.add(tableEntityVO);
                    });
        wordEntityVO.setTableEntityVOS(tableEntityVOS1);
        }



}
